// src/utils/localization.ts
import AsyncStorage from '@react-native-async-storage/async-storage';
import { NativeModules, Platform } from 'react-native';

const deviceLanguage =
  Platform.OS === 'ios'
    ? NativeModules.SettingsManager.settings.AppleLocale ||
    NativeModules.SettingsManager.settings.AppleLanguages[0]
    : NativeModules.I18nManager.localeIdentifier;

export async function configureLocalization() {
  try {
    // 获取保存的语言设置
    let language = await AsyncStorage.getItem('language');

    if (!language) {
      // 使用设备语言
      language = deviceLanguage?.split('_')[0] || 'zh';
      if (language != null) {
        await AsyncStorage.setItem('language', language);
      }
    }

    // 配置i18n或其他本地化库
    console.log(`[Localization] Configured with language: ${language}`);

    return language;
  } catch (error) {
    console.error('[Localization] Configuration error:', error);
    return 'zh';
  }
}